﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
using RainEngine.SceneNodes;

namespace RainEngine.SceneManagement
{
    public sealed class Particle2DManager
    {
        /// <summary>
        /// 引擎引用
        /// </summary>
        private RainGE engine;

        /// <summary>
        /// 所属场景
        /// </summary>
        private Scene scene;

        /// <summary>
        /// 所有控件的集合
        /// </summary>
        private List<Particle2DSystem> particle2DSystems = new List<Particle2DSystem>(); 
       
        /// <summary>
        /// 创建一个新Particle2DManager对象
        /// </summary>
        /// <param name="engine"></param>
        internal Particle2DManager(RainGE engine, Scene setScene)
        {
            this.engine = engine;
            this.scene = setScene;
        }

        public void Draw(GameTime gameTime)
        {
            for (int i = 0; i<particle2DSystems.Count ; i++)
            {
                particle2DSystems[i].Draw(gameTime);
            }
        }

        public void Update(GameTime gameTime)
        {
            for (int i = 0; i<particle2DSystems.Count ; i++)
            {
                particle2DSystems[i].Update(gameTime);
            }
        }

        public void AddParticle2DSystem(Particle2DSystem particle2DSystem)
        {
            particle2DSystems.Add(particle2DSystem);
            particle2DSystems.Sort(drawOrderComparer);
        }

        #region IComparer<T> for scenenodes

        private CompareDrawOrder<Particle2DSystem> drawOrderComparer = new CompareDrawOrder<Particle2DSystem>();

        /// <summary>
        /// Used to sort the SceneManger.activeNodeList. 
        /// Comparison is based on ISceneNode.UpdateOrder. 
        /// </summary>
        private class CompareDrawOrder<T> : IComparer<T> where T : Particle2DSystem
        {
            public int Compare(T x, T y)
            {
                return x.DrawOrder  - y.DrawOrder ;
            }
        }
        #endregion
    }
}
